import { getLoadMapList, getTenantID, requestFunComponFiex } from '@/utils';
import React, { useMemo } from 'react';
import { useRequest } from 'umi';
import { getManageTabSwitch, getSearchList } from '../mconfig';
import { ILoadMapTypeAll } from '@/common';
import { ManageCommonAPI } from '@/pages/type';
import { getBaseUrl } from '@/services/urlconst';

const baseUrl = getBaseUrl();
// const baseUrl = 'http://192.168.153.192:31019'; //振豪本地

/**
 * 管理页面处理数据
 * @param curRes  权限参数
 */
const supplierManageService = (curRes: Record<string, any>) => {
  // /**获取省市区数据 */
  // const areaList = useRequest(() => {
  //   return adminAreadDataGet({});
  // });

  /**  需要获取的数据字典 */
  const loadMdicIds: string[] = [
    `${getTenantID()}${ILoadMapTypeAll.MATERIAL_TYPE}`,
    `${getTenantID()}${ILoadMapTypeAll.PURCHASE_CATEGORY}`,
    `${getTenantID()}${ILoadMapTypeAll.COMPANY_SCALE}`, //企业规模
    `${getTenantID()}${ILoadMapTypeAll.QUALIFICA_TYPE}`, //企业类型
    `${getTenantID()}${ILoadMapTypeAll.COMPANY_TYPE}`, //企业类型
    `${getTenantID()}${ILoadMapTypeAll.SUPPLIER_STATE}`, //认证状态
  ];

  /** 获取数据字典loadMdicMap */
  const loadMdicMapObj: Record<string, any> = getLoadMapList(loadMdicIds);

  /** 获取列表数据  */
  const { tableProps, data, loading, run, refresh } = useRequest(
    (opt: ManageCommonAPI.IGridRequestType) => {
      /**获取权限参数 */
      const queryTypeObj: Record<string, any> = getManageTabSwitch(curRes);
      let creditList: string[] = ['12100000MB2001605R', '12110000MB1N5794X2'];
      const requestBody: Record<string, any> = {
        ...queryTypeObj,
        current: 1,
        pageSize: 10,
        sort: 'createTime',
        order: 'desc',
        dicTypeIds: loadMdicIds?.toString(),
        creditID: creditList?.toString(),
        selectSign: false,
        ...opt,
      };

      const requestURL = `${baseUrl}/supplierInfoController/dataGrid/${requestBody?.params}`;
      return requestFunComponFiex('POST', requestURL, requestBody);
    },
    {
      manual: false, //页面初始请求为 true
      // manual: true, //页面初始请求为 true //模拟数据-王志龙
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /* 获取页面数据 */
  const getSupplierData = (otherRequest: ManageCommonAPI.IGridRequestType | any) => {
    const request: ManageCommonAPI.IGridRequestType | any = {
      ...otherRequest,
    };
    if (request?.createTime) {
      const [createTimeStart, createTimeEnd] = request.createTime;
      request['createTimeStart'] = createTimeStart.format('YYYY-MM-DD 00:00:00');
      request['createTimeEnd'] = createTimeEnd.format('YYYY-MM-DD 23:59:59');
      delete request['createTime'];
    }

    if (request?.belongArea) {
      request['provinceCode'] = request.belongArea[0];
      request['cityCode'] = request.belongArea[1];
      request['countyCode'] = request.belongArea[2];
      delete request['belongArea'];
    }

    delete request['applyFormNameDept'];
    delete request['applyFormNameUser'];
    if (request?.createrID && request?.createrID?.length > 0) {
      request['createrID'] = request?.createrID?.toString();
    } else {
      delete request['createrID'];
    }

    run({
      ...request,
    });
  };

  /**
   * 页面列表
   */
  let returnTableProps = useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  /**行政区域数据 */
  // let areaListData = useMemo(() => {
  //   if (areaList?.data?.msgFlag) {
  //     return areaList?.data?.obj;
  //   } else {
  //     return [];
  //   }
  // }, [areaList?.data]);

  /** 搜索列表  */
  let searchList = React.useMemo(() => {
    return {
      searchList: getSearchList(), //loadMdicMapObj?.loadMapInfo, areaListData
    };
  }, [loadMdicMapObj?.loadMapInfo]); //areaList?.data

  return {
    loading,
    getSupplierData,
    reloadRefresh: refresh,
    ...returnTableProps,
    ...searchList,
  };
};

export { supplierManageService };
